//201902104011王雪梦
package util;
import java.util.List;

public class Condition {
    //不能定义无参构造器，否则fastjson会用它来创建对象
    public Condition(String key,String operator,String value){
        this.key = key;
        this.operator = operator;
        this.value = value;
    }
    public static  String toWhereClause(List<Condition> conditionList){
        StringBuilder sb = new StringBuilder(" WHERE ");
        //将字段名、操作符、字段值依次拼接为字串，每个条件后都附加上" AND "
        for(Condition c:conditionList){
            sb.append(c.getKey()).append(" ").
                    append(c.getOperator()).append(" ");
            String value = c.getValue();
            //如果操作符为like，自动设置为模糊匹配
            if("like".equalsIgnoreCase(c.getOperator())){
                value = "%"+value+"%";
            }
            //把value引起来
            value = StringHelper.quoteMarked(value);
            sb.append(value);
            sb.append(" AND ");
        }
        System.out.println(sb.toString());
        //把最后一个条件后面的and删除
        StringHelper.truncEnding(sb,5);
        return sb.toString();
    }

    //获得字段名
    public String getKey() {
        return key;
    }

    //获得操作符
    public String getOperator() {
        return operator;
    }

    //获得字段值
    public String getValue() {
        return value;
    }

    //字段名
    private String key;
    //操作符
    private String operator;
    //字段值
    private String value;
}
